package zd.cornermemory.utils;


import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class Utils {

	public static String convertStr(String s) {
		if(s.length()==0 || s.equals("0")) return "Error";
		StringBuffer sb = new StringBuffer();
		byte dot = 0, colon = 0;
		int num = 0;
		boolean dbc = false;
		for(int i=0; i<s.length(); i++) {
			if(Character.isDigit(s.charAt(i))) { sb.append(s.charAt(i)); num++; }
			if(s.charAt(i)=='.' && dot<1) { sb.append('.'); dot++; dbc=true; }
			if(s.charAt(i)==':' && colon<2 && !dbc) { sb.append(':'); colon++; }
		}
		if(num == 0) return "Error";
		sb.insert(0, dot + "" + colon);
		return sb.toString();
	}
	
	public static long parseTime(String s) {
		if(s.charAt(1) == '0')
			return (long) (Double.parseDouble(s.substring(2))*1000+0.5);
		long hour, min;
		double sec;
		String[] time = s.substring(2).split(":");
		if(s.charAt(1) == '1') {
			hour = 0;
			min = time[0].length()==0 ? 0 : Long.parseLong(time[0]);
			if(time.length == 1) sec = 0;
			else sec = time[1].length()==0 ? 0 : Double.parseDouble(time[1]);
		}
		else {
			hour = time[0].length()==0 ? 0 : Long.parseLong(time[0]);
			if(time.length == 1) min = 0;
			else min = time[1].length()==0 ? 0 : Long.parseLong(time[1]);
			if(time.length < 3) sec = 0;
			else sec = time[2].length()==0 ? 0 : Double.parseDouble(time[2]);
		}
		return (long) ((hour*3600+min*60+sec)*1000+0.5);
	}

	public static void showKeyboard(final EditText et) {
		new Thread() {
			public void run() {
				try {
					sleep(200);
				} catch (Exception e) { }
				InputMethodManager inm = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
				inm.showSoftInput(et, 0);
			}
		}.start();
	}
	
	public static void hideKeyboard(EditText et) {
		InputMethodManager inm = (InputMethodManager)et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
		inm.hideSoftInputFromWindow(et.getWindowToken(), 0);
	}

}
